#define _CRT_SECURE_NO_WARNINGS 1

# include<bits/stdc++.h>
using namespace std;

map<string, vector<string> > mp;

int main()
{
    int N, M;
    string str, tmp;
    cin >> N;
    getchar();
    for (int i = 0; i < N; ++i)
    {
        getline(cin, str);
        stringstream split(str);
        string index;
        while (split >> tmp)
        {
            index += tmp[0];
        }
        mp[index].push_back(str);
    }
    cin >> M;
    getchar();
    for (int i = 0; i < M; ++i)
    {
        getline(cin, str);
        stringstream split(str);
        string index;
        while (split >> tmp)
        {
            index += tmp[0];
        }
        if (mp.count(index))
        {
            vector<string> v = mp[index];
            sort(v.begin(), v.end());
            for (auto it = v.begin(); it != v.end(); ++it)
            {
                if (it == v.begin())
                {
                    cout << *it;
                }
                else
                {
                    cout << "|" << *it;
                }
            }
            cout << endl;
        }
        else
        {
            cout << str << endl;
        }
    }
    return 0;
}